<template>
	<view class="content">
		<view class="body">
			<up-navbar :title="t('Operating bank cards')" @leftClick="goWithdraw" :autoBack="true">
				<template #left>
				  <view class="nav-back-btn">
				    <image class="back-icon" src="../../../static/img/left.png"></image>
				  </view>
				</template>
			</up-navbar>
			
			<view class="top">
				<u-form>
					<u-form-item>
						<u-input v-model="baseData.cardForm.ifsc" pattern="^\d+$" class="custom-input" inputAlign="right"
							placeholder="Please Enter">
							<template #prefix>
								<up-text :text="t('Withdrawal address')" margin="0 3px 0 0" size="15"></up-text>
							</template>
						</u-input>
					</u-form-item>
				</u-form>
			</view>
		</view>
		<view class="btn" @click="addBank">{{t('Save')}}</view>
		<a :href="baseData.aircraftUrl" target="_blank">
			<img style="width: 2rem;
				height: 2rem;
				position: fixed;
				bottom: 30vh;
				right:6px;
				z-index: 99999;" src="@/static/teltgram.png" />
		</a>
	</view>

</template>

<script setup lang="ts">
	import { useI18n } from 'vue-i18n';
	import { onMounted, reactive,ref } from 'vue'
	import { setBankCard, bankCardInfo } from '@/api/user'

	const { t } = useI18n();
	const baseData = reactive({
		aircraftUrl: uni.getStorageSync('aircraftUrl'),
		cardForm: {
			ifsc: null,
			holder_name: null,
			account_num: null,
			country:0,
			ac_type:3  ,
			platform:100
		}
	})
	const addBank = async () => {
		if (!baseData.cardForm.ifsc) {
			uni.showToast({
				title: t('Please fill in') + t('Withdrawal address'),
				icon: "none"
			})
			return
		}
		// Regular expression to check if the wallet address starts with 'T' (TRX20)
		const trx20Regex = /^T/; // Matches any string that starts with 'T'
		
		if (!trx20Regex.test(baseData.cardForm.ifsc)) {
		    uni.showToast({
		        title: t('Wallet address must start with T for TRX20'),
		        icon: "none"
		    });
		    return;
		}
			baseData.cardForm.account_num='1';
			baseData.cardForm.country=0;
			baseData.cardForm.ac_type=3;
			baseData.cardForm.platform=100;
			baseData.cardForm.holder_name='USDT（TRC20）';
		const res = await setBankCard(baseData.cardForm)
		if (res.code == 0) {
			uni.showToast({
				title: res.msg,
				icon: "none"
			})
			goWithdraw()
		} else {
			uni.showToast({
				title: res.msg,
				icon: "none"
			})
		}
	}
	const goWithdraw = () => {
		uni.reLaunch({
			url: '/pages/withdraw/addBank/bankIndex',
		})
	}
	const getCard = async () => {
		const ress = await bankCardInfo({country:0})
		if (ress.code == 0) {
			baseData.cardForm = ress.data ? ress.data : {
				ifsc: null,
				holder_name: null,
				account_num: null,
			}
		}
	}
	onMounted(() => {
		
		uni.setNavigationBarTitle({
			title: 'task!' + t('Earn money so easy') + '!' // 
		});
		getCard()
	});
</script>

<style lang="scss" scoped>
	
	.content {
		width: 100%;
		background: #F1F1EF;
		min-height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.body {
		width: 100%;
		min-height: 100vh;
		background: #f7f7f7;
	}

	.top {
		border-radius: 1.4rem 1.4rem 0 0;
		padding: 3rem 3vw 0.6rem 3vw;
		// background: linear-gradient(to right, #FE8400, #FF5800);
		background: #ffffff;
		;
		background-size: 100% 130%;
		display: flex;
		flex-flow: column;
	}

	
	.custom-input {
		width: 100%;
		border: none;
		color: #333;
		box-shadow: none;
		outline: none;
	}

	.custom-input:focus {
		border-bottom: 2px solid #ff5800;
	}

</style>